package io.renren.modules.substation.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import io.renren.common.utils.DateUtils;
import io.renren.modules.substation.entity.SelectItemInfo;
import io.renren.modules.substation.entity.TBaseDictInfoEntity;
import io.renren.modules.substation.service.TBaseDictInfoService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.substation.entity.TBusiSysAlarmInfoEntity;
import io.renren.modules.substation.service.TBusiSysAlarmInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 系统告警信息表
 *
 * @author lan
 * @email sunlightcs@gmail.com
 * @date 2020-06-18 20:27:15
 */
@RestController
@RequestMapping("substation/tbusisysalarminfo")
public class TBusiSysAlarmInfoController extends AbstractController {
    @Autowired
    private TBusiSysAlarmInfoService tBusiSysAlarmInfoService;
    @Autowired
    private TBaseDictInfoService tBaseDictInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("substation:tbusisysalarminfo:list")
    public R list(@RequestParam Map<String, Object> params){
        Map<String, Object> data = new HashMap<>();
        PageUtils page = tBusiSysAlarmInfoService.queryPage(params);
        int count = tBusiSysAlarmInfoService.getUnReadCount();//获取未读记录数
        data.put("page", page);
        data.put("count", count);
        return R.ok().put("data", data);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{alarmId}")
    @RequiresPermissions("substation:tbusisysalarminfo:info")
    public R info(@PathVariable("alarmId") Long alarmId){
		TBusiSysAlarmInfoEntity tBusiSysAlarmInfo = tBusiSysAlarmInfoService.getById(alarmId);
        if (tBusiSysAlarmInfo.getReadFlag() == null || "".equals(tBusiSysAlarmInfo.getReadFlag())
                || "0".equals(tBusiSysAlarmInfo.getReadFlag())){// 查看详情后将阅读标志修改为1-已读
            tBusiSysAlarmInfo.setReadFlag("1");
            tBusiSysAlarmInfo.setLastOpr(getUserId());
            tBusiSysAlarmInfo.setLastTime(new Date());
            tBusiSysAlarmInfoService.updateById(tBusiSysAlarmInfo);
        }
        return R.ok().put("tBusiSysAlarmInfo", tBusiSysAlarmInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("substation:tbusisysalarminfo:save")
    public R save(@RequestBody TBusiSysAlarmInfoEntity tBusiSysAlarmInfo){
		tBusiSysAlarmInfoService.save(tBusiSysAlarmInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("substation:tbusisysalarminfo:update")
    public R update(@RequestBody TBusiSysAlarmInfoEntity tBusiSysAlarmInfo){
		tBusiSysAlarmInfoService.updateById(tBusiSysAlarmInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("substation:tbusisysalarminfo:delete")
    public R delete(@RequestBody Long[] alarmIds){
		tBusiSysAlarmInfoService.removeByIds(Arrays.asList(alarmIds));

        return R.ok();
    }

    /**
     * 事件类型列表
     */
    @RequestMapping("/getAlarmTypeList")
    @RequiresPermissions("substation:tbusisysalarminfo:getAlarmTypeList")
    public R getAlarmTypeList(@RequestParam Map<String, Object> params){
        List<SelectItemInfo> retList = new ArrayList<SelectItemInfo>();
        SelectItemInfo temp0=new SelectItemInfo("--请选择--","");
        retList.add(temp0);

        List<TBaseDictInfoEntity> dic_List = tBaseDictInfoService.queryByDictKey("ALARM_TYPE");
        for(int i=0;i<dic_List.size();i++) {
            SelectItemInfo temp=new SelectItemInfo(dic_List.get(i).getDictDesc(),dic_List.get(i).getDictValue());
            retList.add(temp);
        }

        return R.ok().put("list", retList);
    }

}
